package com.example.emojibase.view;

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.emojibase.AuthApiService;
import com.example.emojibase.EmojiBaseDB;
import com.example.emojibase.R;
import com.example.emojibase.RetrofitClient;
import com.example.emojibase.adapter.EmojiAdapter;
import com.example.emojibase.pojo.Emoji;
import com.example.emojibase.pojo.EmojiBase64;
import com.example.emojibase.pojo.Reply;

import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EmojiShowOnline extends AppCompatActivity implements EmojiAdapter.OnItemClickListener{
    private final Context context=this;
    EmojiBaseDB emojiBaseDB;
    EmojiAdapter emojiAdapter;
    private int currentPage=1;
    private final int pageSize=8;
    SQLiteDatabase db;

    private void getEmojiByPage(){
        db=emojiBaseDB.getWritableDatabase();
        AuthApiService service = RetrofitClient.getClient().create(AuthApiService.class);
        service.getEmojiByPage(pageSize,currentPage).enqueue(new Callback<Reply<List<EmojiBase64>>>() {
            @SuppressLint("NewApi")
            @Override
            public void onResponse(@NonNull Call<Reply<List<EmojiBase64>>> call, @NonNull Response<Reply<List<EmojiBase64>>> response) {
                if (response.isSuccessful()) {
                    assert response.body() != null;
                    Reply<List<EmojiBase64>> reply = response.body();
                    if (reply.getStatus() == 1) {
                        List<EmojiBase64> emojis2 = reply.getData();
                        List<Emoji> emojis = new ArrayList<>();
                        for (EmojiBase64 emojiBase64 : emojis2) {
                            emojis.add(new Emoji(emojiBase64.getId(), Base64.getDecoder().decode(emojiBase64.getAvatar()), emojiBase64.getName()));
                        }
                        emojiAdapter.setData(emojis);
                        db.close();
                    } else {
                        Toast.makeText(EmojiShowOnline.this, reply.getMessage(), Toast.LENGTH_SHORT).show();
                        // 处理成功的响应
                    }
                } else {
                    // 处理错误响应
                    Toast.makeText(EmojiShowOnline.this, "Response failed", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(@NonNull Call<Reply<List<EmojiBase64>>> call, @NonNull Throwable t) {
                Log.i(TAG, "" + t);
                // 处理请求失败
                Toast.makeText(EmojiShowOnline.this, "Get response failed", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        emojiBaseDB=EmojiBaseDB.getInstance(this);
        setContentView(R.layout.emoji_show_online);
        Button buttonUp=findViewById(R.id.buttonUp);
        Button buttonDown=findViewById(R.id.buttonDown);
        Button button1=findViewById(R.id.button2);
        RecyclerView recyclerView=findViewById(R.id.emojiRecyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        emojiAdapter=EmojiAdapter.getInstance(context);
        emojiAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(emojiAdapter);
        getEmojiByPage();

        button1.setOnClickListener(view -> {
            Intent intent = new Intent(EmojiShowOnline.this, EmojiShow.class);
            startActivity(intent);
        });

        buttonUp.setOnClickListener(view -> {
            currentPage--;
            getEmojiByPage();
        });

        buttonDown.setOnClickListener(view -> {
            currentPage++;
            getEmojiByPage();
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getEmojiByPage();
    }

    @Override
    public void onDestroy(){
        emojiBaseDB.close();
        super.onDestroy();
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent = new Intent(this, EmojiDetailOnline.class);
        // 可以通过位置position传递数据到新页面
        intent.putExtra("ITEM_POSITION", position);
        startActivity(intent);
    }
}
